package com.xg.controller;

import com.xg.common.constant.CommonStatusEnum;
import com.xg.common.request.SeriesReq;
import com.xg.common.response.UsedCarBrand;
import com.xg.common.response.UsedCarSeries;
import com.xg.common.result.ApiResult;
import com.xg.common.util.Func;
import com.xg.common.util.PageUtil;
import com.xg.service.SeriesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 车型后台接口
 */
@RestController
@RequestMapping("/series")
public class SeriesController {

    @Autowired
    private SeriesService seriesService;

    /**
     * 车系列表
     */
    @GetMapping("/list")
    public ApiResult<List<UsedCarSeries>> list(){
        return seriesService.findUsedCarSeriesList();
    }

    /**
     * 根据brandId查询车系列表
     */
    @GetMapping("/findListByBrandId/{brandId}")
    public ApiResult<List<UsedCarSeries>> findListByBrandId(@PathVariable("brandId")Integer brandId){
        return seriesService.findListByBrandId(brandId);
    }

    /**
     * 根据seriesId查询品牌对象
     */
    @GetMapping("/findBrandBySeriesId/{seriesId}")
    public ApiResult<UsedCarBrand> findBrandBySeriesId(@PathVariable("seriesId")Integer seriesId){
        return seriesService.findBrandBySeriesId(seriesId);
    }

    /**
     * 车系列表分页查询
     */
    @RequestMapping("/queryPageList")
    public ApiResult<PageUtil<UsedCarSeries>> queryPageList(SeriesReq seriesReq) {
        return seriesService.queryPageList(seriesReq);
    }

    /**
     * 车系列表删除结合批量和单个删除
     */
    @RequestMapping("/delete")
    public ApiResult deleteSerices(@RequestBody List<Integer> ids) {
        if (!Func.isEmpty(ids)) {
            return seriesService.deleteSerices(ids);
        }
        return ApiResult.error(CommonStatusEnum.SERIES_DELETE_ERROR.getCode(), CommonStatusEnum.SERIES_DELETE_ERROR.getMsg());
    }

    /**
     * 车系查询单个品牌信息
     */
    @RequestMapping("/info/{seriesId}")
    public ApiResult<UsedCarSeries> selectSeriesInfo(@PathVariable("seriesId") Integer seriesId) {
        if (!Func.isEmpty(seriesId)) {
            return seriesService.selectSeriesInfo(seriesId);
        }
        return ApiResult.error(CommonStatusEnum.SERIES_INFO_ERROR.getCode(), CommonStatusEnum.SERIES_INFO_ERROR.getMsg());
    }


    /**
     * 车系信息新增和修改
     */
    @RequestMapping("/save")
    public ApiResult saveSeries(@RequestBody UsedCarSeries usedCarSeries) {
        if (!Func.isEmpty(usedCarSeries)) {
            return seriesService.saveSeries(usedCarSeries);
        }
        return ApiResult.error(CommonStatusEnum.SERIES_SAVE_ERROR.getCode(), CommonStatusEnum.SERIES_SAVE_ERROR.getMsg());
    }


}
